#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# @Time    : 2018/10/3 22:40
# @Author  : liujiantao
# @Site    : https://toutiao.io/posts/y5vljj/preview
# @File    : blending_model.py
# @Software: PyCharm
import random

import pandas as pd
import numpy as np

print("start")
# 基础配置信息
path = '../data/submmit/blend/'
res01 = path + "0.719.csv"
# res02 = path + "cor_arma_predict_02.csv"
res03 = path + "0.691741.csv"
res04 = path + "0.65.csv"
# res05 = path + "0.565.csv"
# res06 = path + "sub_tyf_time_win_regress_v1.csv"
# res07 = path + "cor_arma_predict_02_v3.csv"
print("基础配置信息")
res01_df = pd.read_csv(res01)
res01_df.columns = ['UID', 'Tag']
# res02_df = pd.read_csv(res02)
# res02_df.columns = ['ID', 'V2']
res03_df = pd.read_csv(res03)
res03_df.columns = ['UID', 'Tag']
res04_df = pd.read_csv(res04)
res04_df.columns = ['UID', 'Tag']
# res05_df = pd.read_csv(res05)
# res05_df.columns = ['UID', 'V5']
# res06_df = pd.read_csv(res06)
# res06_df.columns = ['UID', 'V6']
# res07_df = pd.read_csv(res07)
# res07_df.columns = ['UID', 'V7']

# res = pd.merge(res01_df, res02_df, on='ID')
res = pd.merge(res01_df, res03_df, on='UID')
res = pd.merge(res, res04_df, on='UID')
# res = pd.merge(res, res05_df, on='UID')
# res = pd.merge(res, res06_df, on='ID')
# res = pd.merge(res, res07_df, on='ID')
weights = [0.719,  0.691741, 0.65]

ID = []
value = []


def weight_average(df):
    ID.append(df[0])
    val = df[1:].tolist()
    v = np.average(val)
    # v = np.average(val, weights=weights)
    value.append(v)


res.T.apply(weight_average)
# blend_model = pd.DataFrame({
#     'UID': ID,
#     'Tag': value
# })
res01_df['Tag'] = value
rule_code = [ '5776870b5747e14e' ,'8b3f74a1391b5427' ,'0e90f47392008def' ,'6d55ccc689b910ee' ,'2260d61b622795fb' ,'1f72814f76a984fa' ,'c2e87787a76836e0' ,'4bca6018239c6201' ,'922720f3827ccef8' ,'2b2e7046145d9517' ,'09f911b8dc5dfc32' ,'7cc961258f4dce9c' ,'bc0213f01c5023ac' ,'0316dca8cc63cc17' ,'c988e79f00cc2dc0' ,'d0b1218bae116267' ,'72fac912326004ee' ,'00159b7cc2f1dfc8' ,'49ec5883ba0c1b0e' ,'c9c29fc3d44a1d7b' ,'33ce9c3877281764' ,'e7c929127cdefadb' ,'05bc3e22c112c8c9' ,'5cf4f55246093ccf' ,'6704d8d8d5965303' ,'4df1708c5827264d' ,'6e8b399ffe2d1e80' ,'f65104453e0b1d10' ,'1733ddb502eb3923' ,'a086f47f681ad851' ,'1d4372ca8a38cd1f' ,'29db08e2284ea103' ,'4e286438d39a6bd4' ,'54cb3985d0380ca4' ,'6b64437be7590eb0' ,'89eb97474a6cb3c6' ,'95d506c0e49a492c' ,'c17b47056178e2bb' ,'d36b25a74285bebb']
print(len(rule_code))
Test_trans = pd.read_csv('../data/transaction_round1_new.txt')
test_rule_uid = pd.DataFrame(Test_trans[Test_trans['merchant'].isin(rule_code)].UID.unique())
pred_data_rule = res01_df.merge(test_rule_uid,left_on ='UID',right_on =0, how ='left')
res01_df['Tag'][(pred_data_rule[0]>0)] = random.uniform(0.9,1.0) # 这个系数还需要调整
res01_df.to_csv(path+'blending02.csv', index=False)
print(123)

